#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<unistd.h>
#include<stdlib.h>
#define PATH "/home/study/lesson8"
#define ID 0x6666
#define SIZE 4096

int main()
{
    key_t key = ftok(PATH, ID);
    if(key < 0){
        perror("ftok!\n");
        exit(1);
    }

    //创建共享内存区
    int shm = shmget(key, SIZE, IPC_CREAT | IPC_EXCL | 0666);
    if(shm < 0){
        perror("shmget!\n");
        exit(2);
    }

    printf("the key is : %d\n", key);
    printf("the shm is : %d\n", shm);

    //关联内存和虚拟地址
    char* mem = (char*)shmat(shm, NULL, 0);
    if(mem == (void*) - 1){
        perror("shmat!\n");
        exit(1);    
    }else printf("shmat success!\n");

    while(1)
    {
        printf("client# %s\n", mem);
        sleep(1);
    }

    sleep(2);
    shmdt(mem);
    printf("dttach success!\n");

    shmcctl(shm, IPC_RMID, NULL);
    return 0;



}